package com.webfinal.backsystem.service.impl;

import com.webfinal.backsystem.dao.ActivityMapper;
import com.webfinal.backsystem.dao.ActivityProductRelMapper;
import com.webfinal.backsystem.dao.ActivityProductsMapper;
import com.webfinal.backsystem.dao.ProductMapper;
import com.webfinal.backsystem.entity.Activity;
import com.webfinal.backsystem.entity.ActivityProductRel;
import com.webfinal.backsystem.entity.ActivityProducts;
import com.webfinal.backsystem.entity.Product;
import com.webfinal.backsystem.request.activity.ActivityInfoRequest;
import com.webfinal.backsystem.request.activity.ActivityProdRequest;
import com.webfinal.backsystem.request.activity.AddActivityProdRequest;
import com.webfinal.backsystem.response.activity.ActivityInfoResponse;
import com.webfinal.backsystem.response.activity.ActivityProdResponse;
import com.webfinal.backsystem.response.activity.AddActivityProdResponse;
import com.webfinal.backsystem.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityProductsMapper activityProductsMapper;

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ActivityProductRelMapper activityProductRelMapper;

    @Override
    public ActivityInfoResponse allactivity() {
        ActivityInfoResponse response = new ActivityInfoResponse();
        List<Activity> activityList=activityMapper.selectAll();
        if(activityList==null){
            response.setResultCode("1");
            response.setResultMsg("error");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setActivityList(activityList);
        }
        return response;
    }

    @Override
    public ActivityInfoResponse activitybyname(ActivityInfoRequest activityInfoRequest) {
        ActivityInfoResponse response = new ActivityInfoResponse();
        List<Activity> activityList=activityMapper.selectByname(activityInfoRequest.getActivityName());
        if(activityList==null){
            response.setResultCode("1");
            response.setResultMsg("error");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setActivityList(activityList);
            System.out.println(activityList);
        }
        return response;
    }
    public static Date dealDateFormat(String oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
            oldDate= oldDate.replace("Z", " UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }
    @Override
    public ActivityInfoResponse addactivity(ActivityInfoRequest activityInfoRequest) {
        ActivityInfoResponse response = new ActivityInfoResponse();
        Activity activity = new Activity();
        activity.setActivityId(0);
        activity.setActivityCode(activityInfoRequest.getActivityCode());
        activity.setActivityName(activityInfoRequest.getActivityName());
        Date date=dealDateFormat(activityInfoRequest.getStartDate());
        Date date1=dealDateFormat(activityInfoRequest.getEndDate());
        activity.setStartDate(date);
        activity.setEndDate(date1);

        if(activityMapper.insert(activity)!=1){
            response.setResultCode("1");
            response.setResultMsg("error");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
        }
        return response;
    }

    @Override
    public ActivityProdResponse searchactivityprod(ActivityProdRequest activityProdRequest) {
        ActivityProdResponse response = new ActivityProdResponse();
        List<ActivityProducts> activityProductsList = activityProductsMapper.selectbycode(activityProdRequest.getActivityCode());
        if(activityProductsList!=null){
            response.setResultCode("0");
            response.setResultMsg("success");
            response.setActivityProductsList(activityProductsList);
        }else {
            response.setResultCode("1");
            response.setResultMsg("error");
        }
        return response;
    }

    @Override
    public AddActivityProdResponse addactivityprod(AddActivityProdRequest addActivityProdRequest) {
        AddActivityProdResponse response = new AddActivityProdResponse();
        ActivityProductRel activityProductRel = new ActivityProductRel();
        Product product =new Product();
        product = productMapper.selectByProName(addActivityProdRequest.getProductName());
        activityProductRel.setActivityCode(addActivityProdRequest.getActivityCode());
        activityProductRel.setProductId(product.getProductId());
        activityProductRel.setProductName(product.getProductName());
        activityProductRel.setPrice(product.getProductPrice());
        activityProductRel.setProductImage(product.getImage());
        if(activityProductRelMapper.insert(activityProductRel)!=1){
            response.setResultCode("1");
            response.setResultMsg("error");
        }else {
            response.setResultCode("0");
            response.setResultMsg("success");
        }
        return response;
    }
}
